//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YYGenericCPIr.h"
using namespace Cephei::QL::Experimental::Inflation;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Termstructures/YoYInflationTermStructure.h>
#include <gen/QL/Indexes/Region.h>
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (QL::Times::FrequencyEnum frequency, Boolean revised, Boolean interpolated, Cephei::QL::Times::IPeriod^ lag, Cephei::QL::ICurrency^ ccy, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>^ ts) : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
    CPeriod^ _Clag;
    CCurrency^ _Cccy;
    CYoYInflationTermStructure^ _Cts;
    try
    {
#ifdef HANDLE
        _phYYGenericCPIr = NULL;
#endif
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        bool _revised = (bool)ValueHelper::Convert (revised);
        bool _interpolated = (bool)ValueHelper::Convert (interpolated);
        _Clag = safe_cast<CPeriod^> (lag);
        _Clag->Lock();
        QuantLib::Period& _lag = static_cast<QuantLib::Period&> (_Clag->GetReference ()); 
        _Cccy = safe_cast<CCurrency^> (ccy);
        _Cccy->Lock();
        QuantLib::Currency& _ccy = static_cast<QuantLib::Currency&> (_Cccy->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>::IsSome::get (ts))
        {
            _Cts = safe_cast<CYoYInflationTermStructure^> (ts->Value);
            _Cts->Lock();
        }
        Handle<QuantLib::YoYInflationTermStructure>& _ts = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>::IsSome::get (ts) ? static_cast<Handle<QuantLib::YoYInflationTermStructure>&> (_Cts->GetHandle ()) : Handle<QuantLib::YoYInflationTermStructure>()); //1
        _ppYYGenericCPIr = new boost::shared_ptr<QuantLib::YYGenericCPIr> (new QuantLib::YYGenericCPIr ( _frequency,  _revised,  _interpolated,  _lag,  _ccy,  _ts ));
        SetYoYInflationIndex (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Clag != nullptr) _Clag->Unlock();
        if (_Cccy != nullptr) _Cccy->Unlock();
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (boost::shared_ptr<QuantLib::YYGenericCPIr>& childNative, Object^ owner) : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
#ifdef HANDLE
	_phYYGenericCPIr = NULL;
#endif
	_ppYYGenericCPIr = &childNative;
    _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
}
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (QuantLib::YYGenericCPIr& childNative, Object^ owner) : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
#ifdef HANDLE
	_phYYGenericCPIr = NULL;
#endif
	_ppYYGenericCPIr = new boost::shared_ptr<QuantLib::YYGenericCPIr> (&childNative);
    _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
    _YYGenericCPIrOwner = owner;
    _YoYInflationIndexOwner = owner;
}

Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (CYYGenericCPIr^ copy) : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
#ifdef HANDLE
	_phYYGenericCPIr = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYYGenericCPIr = new boost::shared_ptr<QuantLib::YYGenericCPIr> (copy->GetShared());
        _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
    }
}
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (System::Type^ t) : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
#ifdef HANDLE
	_phYYGenericCPIr = NULL;
#endif
	if (!t->IsSubclassOf(CYYGenericCPIr::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (QuantLib::Handle<QuantLib::YYGenericCPIr>& childNative, Object^ owner)  : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
	_phYYGenericCPIr = &childNative;
	_ppYYGenericCPIr = &static_cast<boost::shared_ptr<QuantLib::YYGenericCPIr>>(childNative.currentLink());
    _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
    _YYGenericCPIrOwner = owner;
}
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (QuantLib::Handle<QuantLib::YYGenericCPIr> childNative)  : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
	_phYYGenericCPIr = &childNative;
	_ppYYGenericCPIr = &static_cast<boost::shared_ptr<QuantLib::YYGenericCPIr>>(childNative.currentLink());
    _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::CYYGenericCPIr (QuantLib::YYGenericCPIr childNative)  : CYoYInflationIndex(CYYGenericCPIr::typeid)
{
#ifdef HANDLE
	_phYYGenericCPIr = NULL;
#endif
	_ppYYGenericCPIr = new boost::shared_ptr<QuantLib::YYGenericCPIr> (new QuantLib::YYGenericCPIr (childNative));
    _ppYoYInflationIndex = new boost::shared_ptr<QuantLib::YoYInflationIndex> (boost::dynamic_pointer_cast<QuantLib::YoYInflationIndex> (*_ppYYGenericCPIr));
}
#endif

Cephei::QL::Experimental::Inflation::CYYGenericCPIr::~CYYGenericCPIr ()
{
    if (_ppYYGenericCPIr != NULL)
    {
	    delete _ppYYGenericCPIr;
        _ppYYGenericCPIr = NULL;
    }
}
Cephei::QL::Experimental::Inflation::CYYGenericCPIr::!CYYGenericCPIr ()
{
    if (_ppYYGenericCPIr != NULL)
    {
	    delete _ppYYGenericCPIr;
    }
}
QuantLib::YYGenericCPIr& Cephei::QL::Experimental::Inflation::CYYGenericCPIr::GetReference ()
{
    if (_ppYYGenericCPIr == NULL) throw gcnew NativeNullException ();
	return **_ppYYGenericCPIr;
}
boost::shared_ptr<QuantLib::YYGenericCPIr>& Cephei::QL::Experimental::Inflation::CYYGenericCPIr::GetShared ()
{
    if (_ppYYGenericCPIr == NULL) throw gcnew NativeNullException ();
	return *_ppYYGenericCPIr;
}
QuantLib::YYGenericCPIr* Cephei::QL::Experimental::Inflation::CYYGenericCPIr::GetPointer ()
{
    if (_ppYYGenericCPIr == NULL) throw gcnew NativeNullException ();
	return &**_ppYYGenericCPIr;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YYGenericCPIr>& Cephei::QL::Experimental::Inflation::CYYGenericCPIr::GetHandle ()
{
	if (_phYYGenericCPIr == NULL)
	{
		_phYYGenericCPIr = new Handle<QuantLib::YYGenericCPIr> (*_ppYYGenericCPIr);
	}
	return *_phYYGenericCPIr;
}
#endif
bool Cephei::QL::Experimental::Inflation::CYYGenericCPIr::HasNative () 
{
	return (_ppYYGenericCPIr != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Inflation::IYYGenericCPIr^ Cephei::QL::Experimental::Inflation::CYYGenericCPIr_Factory::Create (QL::Times::FrequencyEnum frequency, Boolean revised, Boolean interpolated, Cephei::QL::Times::IPeriod^ lag, Cephei::QL::ICurrency^ ccy, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYoYInflationTermStructure^>^ ts)
{
    return gcnew CYYGenericCPIr ( frequency,  revised,  interpolated,  lag,  ccy,  ts);
}
